package com.nl.controller.xuyi.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.xuyi.bean.xuyiline.XuYiLineInfo;
import com.nl.controller.xuyi.bean.xuyiline.QryLineInfoReq;
import com.nl.controller.xuyi.bean.xuyiline.QryLineInfoWithMgrIdReq;
import com.nl.controller.xuyi.service.XuYiLineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangheng
 * @Desc:
 * @date: 15:16 2020/9/1
 * @Modified By:
 */
@RestController
@RequestMapping("/xuYiLine")
@Slf4j
public class XuYiLineController {
    @Autowired
    private XuYiLineService xuYiLineService;
    /**
     * desc: 点位信息查询
     * @param req:  
     * @return: com.nl.controller.common.model.Response
     * @author zhangheng
     * @date 2020/9/1 15:41
     */
    @RequestMapping("/qryLineInfo")
    public Response qryLineInfo(@RequestBody QryLineInfoReq req){
        log.info("XuYiLineController qryLineInfo 点位信息查询 start request: "+ JSON.toJSONString(req));
        Response response = xuYiLineService.qryLineInfo(req);
        log.info("XuYiLineController qryLineInfo 点位信息查询 end response: "+ JSON.toJSONString(response));
        return response;
    }

    
    
    /**
     * desc: 点位信息查询
     * @param req:
     * @return: com.nl.controller.common.model.Response
     * @author zhangheng
     * @date 2020/9/1 15:41
     */
    @RequestMapping("/qryLineInfoNew")
    public Response qryLineInfoNew(@RequestBody QryLineInfoReq req){
        log.info("XuYiLineController qryLineInfo 点位信息查询 start request: "+ JSON.toJSONString(req));
        Response response = xuYiLineService.qryLineInfoNew(req);
        log.info("XuYiLineController qryLineInfo 点位信息查询 end response: "+ JSON.toJSONString(response));
        return response;
    }
    
    @RequestMapping("/qryLineInfoWithMgrId")
    public Response qryLineInfoWithMgrId(@RequestBody QryLineInfoWithMgrIdReq req){
        log.info("XuYiLineController qryLineInfo 点位信息查询 start request: "+ JSON.toJSONString(req));
        Response response = xuYiLineService.qryLineInfoWithMgrId(req);
        log.info("XuYiLineController qryLineInfo 点位信息查询 end response: "+ JSON.toJSONString(response));
        return response;
    }


    @PostMapping("/findLinesInfo")
    public Response<XuYiLineInfo> findLinesInfo(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "lineName", required = false) String pathName
    ) {
        PageInfo<XuYiLineInfo> pageInfo  = xuYiLineService.findLinesInfo(page,limit,pathName);
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }


    @PostMapping("findLineNameList")
    public Response<String> findLineNameList(@RequestParam(value = "lineName", required = false) String lineName
    ) {
        List<String> list = new ArrayList<String>();
        list = xuYiLineService.findLineNameList(lineName);
        log.info("点位名称联想参数：{}", lineName);
        log.debug("点位名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
    @PostMapping("lineRelPointAdd")
    public Response lineRelPointAdd(
            @RequestParam(value = "lineName", required = false) String lineName	,
            @RequestParam(value = "pointTips", required = false) String pointNameList,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        log.info("新增路线信息插入审核表：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();

        int result = 0;

        result = xuYiLineService.lineRelPointAdd(lineName,pointNameList,operatorTel);

        log.debug("新增路线信息插入审核表返回数据：{}", result);
        if (result > 0) {
            return ResponseUtil.sucess("路线新增成功，待审核！");
        } else {
            return ResponseUtil.error(result, "路线新增失败，请重试");
        }
    }

    @PostMapping("lineRelPointModify")
    public Response lineRelPointModify(
            @RequestParam(value = "lineId", required = false) String lineId	,
            @RequestParam(value = "lineName", required = false) String lineName	,
            @RequestParam(value = "pointTips", required = false) String pointNameList,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        log.info("路线变更插入审核表：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();

        int result = 0;

        result = xuYiLineService.lineRelPointModify(lineId,lineName,pointNameList,operatorTel);

        log.debug("路线变更插入审核表返回数据：{}", result);
        if (result > 0) {
            return ResponseUtil.sucess("路线信息修改成功，待审核！");
        } else {
            return ResponseUtil.error(result, "路线信息修改失败，请重试");
        }
    }


    @PostMapping("findLineInfoById")
    public Response<String> findLineInfoById(@RequestParam(value = "lineId", required = false) String lineId
    ) {
        XuYiLineInfo xuYiLineInfo = xuYiLineService.findLineInfoById(lineId);
        log.info("根据路线id查询路线信息：{}", lineId);
        log.info("根据路线id查询路线信息返回数据：{}", xuYiLineInfo.toString());
        return ResponseUtil.sucess(xuYiLineInfo);
    }

    @PostMapping("findPointRelByLineId")
    public Response<List<String>> findPointRelByLineId(@RequestParam(value = "lineId", required = false) String lineId
    ) {
        List<String> pointNamesList= xuYiLineService.findPointRelByLineId(lineId);
        log.info("根据路线id查询关联的点位名称：{}", lineId);
        log.info("根据路线id查询关联的点位名称：{}", pointNamesList.toString());
        return ResponseUtil.sucess(pointNamesList);
    }

    @PostMapping("deleteLineInfo")
    private Response deleteLineById(
            @RequestParam(value = "lineId") String lineId,
            @RequestParam(value = "lineName") String lineName,
            HttpSession session,
            @RequestParam Map<String, Object> paramMap) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();
        int result = xuYiLineService.deleteLine(lineId,lineName,operatorTel);
        if(result>0){
            log.info("删除路线信息:{}" ,paramMap.toString());
            log.info("删除路线信息：",result);
            return ResponseUtil.sucess("删除路线信息成功，待审核！");
        } else {
            log.error("删除路线信息：{}", result);
            return ResponseUtil.error(-1, "删除路线信息失败，请重试");
        }

    }

    @PostMapping("/findLineInfoByName")
    public Response<XuYiLineInfo> findLinesInfo(
            @RequestParam(value = "lineName", required = false) String pathName,
            @RequestParam Map<String, Object> paramMap
    ) {
        log.info("根据名称查询点位信息:{}" ,paramMap.toString());
        List<XuYiLineInfo> lindInfos  = xuYiLineService.findLinesInfoByName(pathName);
        log.info("根据名称查询点位信息返回结果:{}" ,lindInfos.toString());
        return ResponseUtil.sucess(lindInfos);
    }


}
